package org.example.service.impl;

import cn.hutool.core.lang.hash.Hash;
import cn.hutool.core.util.IdUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.aspectj.weaver.ast.Or;
import org.example.common.Result;
import org.example.mapper.OrderitemMapper;
import org.example.pojo.*;
import org.example.service.CartService;
import org.example.service.OrderService;
import org.example.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author 13326
* @description 针对表【hards_order】的数据库操作Service实现
* @createDate 2025-01-10 18:25:59
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderitemMapper orderitemMapper;
@Resource
private CartService cartService;
    @Override

    public Result add(OrderTO orderTO){
        Order order1=new Order();
        System.out.println(order1);
        order1.setOrderNo(IdUtil.getSnowflake().nextIdStr());
        System.out.println(order1.getOrderNo());
        order1.setAddress(orderTO.getAddress());
        order1.setAmount(orderTO.getAmount());
        order1.setComment(orderTO.getComment());
        order1.setBusinessId(orderTO.getBusinessId());
        order1.setUser(orderTO.getUser());
        order1.setStatus("待支付");
       order1.setActual(orderTO.getActual());
        order1.setUserId(orderTO.getUserId());
        order1.setPhone(orderTO.getPhone());
orderMapper.insert(order1);

        System.out.println(orderTO.getId());
        for (int i=0;i<orderTO.getCarts().length;i++){
            Orderitem orderitem=new Orderitem();
            orderitem.setOrderId(order1.getId());
            orderitem.setGoodsName(orderTO.getCarts()[i].getGname());
            orderitem.setNum(orderTO.getCarts()[i].getNum());
            orderitem.setGoodsImg(orderTO.getCarts()[i].getImg());
            orderitem.setPrice(orderTO.getCarts()[i].getPrice());
        orderitemMapper.insert(orderitem);
        }
        cartService.clear(orderTO.getUserId(),orderTO.getBusinessId());

        return Result.ok("新增成功");

    }
@Override
    public Result getall(String status,Integer userId){
    System.out.println(status);
        if(status.equals("全部")){
            System.out.println("2");
            LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();

            lambdaQueryWrapper.eq(Order::getUserId,userId);

            List<Order> orders=orderMapper.selectList(lambdaQueryWrapper);
            System.out.println(orders);
            for (int i = 0; i < orders.size(); i++) {
                LambdaQueryWrapper<Orderitem> lan1=new LambdaQueryWrapper<>();
                lan1.eq(Orderitem::getOrderId,orders.get(i).getId());
                List<Orderitem> list1=orderitemMapper.selectList(lan1);
                orders.get(i).setName(list1.get(0).getGoodsName()+"等"+list1.size()+"件餐品");
            orders.get(i).setCover(list1.get(0).getGoodsImg());
            }
            return Result.ok(orders);
        }else{
            System.out.println("1");
            LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Order::getUserId,userId);
            lambdaQueryWrapper.eq(Order::getStatus,status);
            List<Order> orders=orderMapper.selectList(lambdaQueryWrapper);
            for (int i = 0; i < orders.size(); i++) {
                LambdaQueryWrapper<Orderitem> lan1=new LambdaQueryWrapper<>();
                lan1.eq(Orderitem::getOrderId,orders.get(i).getId());
                List<Orderitem> list1=orderitemMapper.selectList(lan1);
                orders.get(i).setName(list1.get(0).getGoodsName()+"等"+list1.size()+"件餐品");
                orders.get(i).setCover(list1.get(0).getGoodsImg());
            }
            return Result.ok(orders);

        }
}
@Override
    public Result updateorder(Order order){
        int rows=orderMapper.updateById(order);
        return Result.ok("修改成功");
}
@Override
    public Result shanchu(Integer id){
        int rows=orderMapper.deleteById(id);
        return Result.ok("删除成功");
}
@Override
    public Result detail(Integer id){
        Order order=orderMapper.selectById(id);
    HashMap<String,Object> hashMap=new HashMap<>();
    hashMap.put("data",order);
    LambdaQueryWrapper<Orderitem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(Orderitem::getOrderId,id);
    List<Orderitem> list=orderitemMapper.selectList(lambdaQueryWrapper);
    hashMap.put("list",list);
    return Result.ok(hashMap);
}
@Override
    public Result selectpage(Query query){
        LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper
            .like(!StringUtils.isEmpty(query.getKeyword()), Order::getName,query.getKeyword())
            .eq(Order::getName,query.getKeyword());
    IPage<Order> page=new Page<>(query.getPageNum(),query.getPageSize());
    orderMapper.selectPageMap(page,query);
    HashMap<String,Object> map=new HashMap<>();
    map.put("total",page.getTotal());
    map.put("rows",page.getRecords());
    map.put("pages",page.getPages());
    map.put("size",page.getSize());
    return Result.ok(map);


}
}




